﻿# -*- coding: utf-8 -*-
from django.db import models
from django.core.urlresolvers import reverse
from Colaboradores.models import Colaborador
from Scope.models import ImageWithThumbsField
class Blog(models.Model):
    nome = models.CharField(max_length=20, blank=True, null=True)
    dono = models.ForeignKey(Colaborador)
    igreja = models.CharField(max_length=50, blank=True, null=True)
    email = models.CharField('Email do Blog', max_length=30)
    descricao = models.CharField(max_length=255, blank=True, null=True)
    imagem = ImageWithThumbsField('Imagem Destaque', upload_to='arquivos/blogs/', sizes=((620,465),(180,123)))
    slugdoblog = models.SlugField(max_length=255, blank=True, unique=True)
    datapublicacao = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return self.nome
    def get_absolute_url(self):
        return reverse('Scope.views.blog', kwargs={'slug': self.slug})

class GaleriaFotos(models.Model):
    titulo = models.CharField('Título',max_length=50)
    imagem = models.ImageField(upload_to='arquivos/galeria')
    data = models.DateTimeField('Data de publicação',auto_now_add=True)
    autor = models.CharField(max_length=50)
    descricao = models.TextField('Descricao')

    class Meta:
        verbose_name_plural = 'Galeria de imagens'
    def __unicode__(self):
        return self.titulo

def content_image_name(instance, filename):
    return '/'.join(['arquivosblog', str(instance.nomedoblog.slugdoblog), filename])

class NoticiaBlog(models.Model):
    publicar = models.BooleanField(default=True)
    titulo = models.CharField(max_length=100, unique=True)
    subtitulo = models.CharField(max_length=200)
    chamada = models.CharField(max_length=50)
    conteudo = models.TextField(blank=True, null=True)
    datapublicacao = models.DateTimeField(auto_now_add=True)
    imagemDestaque = ImageWithThumbsField('Imagem Destaque', upload_to=content_image_name, sizes=((620,465),(180,124)))
    usarVideo = models.BooleanField(default=False)
    video = models.URLField(null=True, blank=True)
    descricaovideo = models.CharField(max_length=500, blank=True, null=True)
    usarGaleria = models.BooleanField(default=False)
    galeria = models.ManyToManyField(GaleriaFotos, null=True, blank=True)
    slug = models.SlugField(max_length=255, blank=True, unique=True)
    autor = models.ForeignKey(Colaborador)
    nomedoblog = models.ForeignKey(Blog)
    cliques = models.IntegerField('Cliques', default=0, editable=False)
    def __unicode__(self):
        return self.titulo
    def get_absolute_url(self):
        return reverse('Scope.views.blognoticia', kwargs={'nomedoblog': self.nomedoblog.slugdoblog, 'slug': self.slug})

# SIGNALS
from django.db.models import signals
from django.template.defaultfilters import slugify

def blog_pre_save(signal, instance, sender, **kwargs):
    """
    Estamos gerando uma nova slug para evitar duplicidade
    """
    if not instance.slugdoblog:
        slug = slugify(instance.nome)
        novaslug = slug
        contador = 0

        while  Blog.objects.filter(slugdoblog = novaslug).exclude(id=instance.id).count() > 0:
            contador += 1
            novaslug = '%s-%d'%(slug, contador)
        instance.slugdoblog = novaslug
signals.pre_save.connect(blog_pre_save, sender=Blog)

def noticiablog_pre_save(signal, instance, sender, **kwargs):
    """
    Estamos gerando uma nova slug para evitar duplicidade
    """
    if not instance.slug:
        slug = slugify(instance.titulo)
        novaslug = slug
        contador = 0

        while  NoticiaBlog.objects.filter(slug = novaslug).exclude(id=instance.id).count() > 0:
            contador += 1
            novaslug = '%s-%d'%(slug, contador)
        instance.slug = novaslug
signals.pre_save.connect(noticiablog_pre_save, sender=NoticiaBlog)